package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端传递过来的密码进行md5加密，再匹配
         password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //3、返回实体对象
        return employee;
    }

    /**
     * 新增员工
     * @param EmployeeDTO
     */
    @Override
    public void addEmployee(EmployeeDTO EmployeeDTO) {
        Employee employee = new Employee();
        //拷贝对象
        BeanUtils.copyProperties(EmployeeDTO,employee);

        //设置状态
        employee.setStatus(StatusConstant.ENABLE);
        //设置密码，需要md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //这些共有属性使用了aop公共字段自动填充
        //设置创建时间和，更新时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //设置创建人id，修改人id
        //setCreateUser(BaseContext.getCurrentId());
        //employee.setUpdateUser(BaseContext.getCurrentId());

        log.info("employee:{}",employee);
        employeeMapper.addEmployee(employee);

    }

    /**
     * 员工分页查询
     * @return
     */
    @Override
    public PageResult empList(EmployeePageQueryDTO pageQueryDTO) {
        //底层是 select * from employee limit 1,10;
        //使用pageHelper分页
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        //查询所有员工
        Page<Employee> page = employeeMapper.getEmpList(pageQueryDTO);
        //将分页信息存储
        PageResult pageResult = new PageResult(page.getTotal(),page.getResult());

        return pageResult;
    }

    /**
     * 启用禁用员工
     * @param id,status
     */
    @Override
    public void serStatus(Integer status,Long id) {
        //原来的写法
        //Employee employee = new Employee();
//        employee.setStatus(status);
//        employee.setId(id);

        //新写法
        Employee employee = Employee.builder()
                .status(status)
                .id(id)
                .build();
        employeeMapper.update(employee);
    }

    /**
     * 回显员工
     * @param id
     * @return
     */
    @Override
    public Employee quire(Long id) {
        Employee employee = employeeMapper.quire(id);
        return employee;
    }

    /**
     * 修改员工
     * @param employeeDTO
     */
    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        //拷贝对象
        BeanUtils.copyProperties(employeeDTO,employee);

        //已使用aop自动填充
        //设置修改时间
        //employee.setUpdateTime(LocalDateTime.now());
        //设置修改人
        //employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }
}
